package com.wh.wisdomsite.xbox.server;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * 构建redis连接池
 * @Package com.wh.wisdomsite.xbox.server
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class ShardedJedisPoolUtils {

    private static final Log logger = LogFactory.getLog(ShardedJedisPoolUtils.class);
	
	private static ShardedJedisPool pool;
	private static int REDIS_POOL_MAXACTIVE ;    //最大连接数
	private static int REDIS_POOL_MAXIDLE ;		   //最大空闲连接数
	private static int REDIS_POOL_MINIDLE;			  //初始化连接数
	private static long REDIS_POOL_MAXWAIT ;   //最大等待时间
	private static boolean REDIS_POOL_TESTONBORROW;  //对拿到的connection进行validateObject校验
	private static boolean REDIS_POOL_TESTONRETURN;   //在进行returnObject对返回的connection进行validateObject校验
	private static boolean REDIS_POOL_TESTWHILEIDLE;         //定时对线程池中空闲的链接进行validateObject校验
	
	private static String  REDIS_SERVER_ONE_ADRESS;
	private static int REDIS_SERVERIP_ONE_PORT;
	private static String  REDIS_SERVERIP_ONE_PWD;
	
	private static String  REDIS_SERVER_TWO_ADRESS;
	private static int REDIS_SERVERIP_TWO_PORT;
	private static String  REDIS_SERVERIP_TWO_PWD;
	
	private static List<JedisShardInfo> shardInfos = new LinkedList<JedisShardInfo>();
	
	/**
	 * 从配置文件读取redis 配置信息
	 */
	static {
		ResourceBundle bundle = ResourceBundle.getBundle("config/redis");
		if (bundle == null) {
			throw new IllegalArgumentException("[redis.properties] is not found!");
		}
		try {
		// 池基本配置 
		REDIS_POOL_MAXACTIVE = Integer.valueOf(bundle.getString("redis.pool.maxActive"));
		REDIS_POOL_MAXIDLE = Integer.valueOf(bundle	.getString("redis.pool.maxIdle"));
		REDIS_POOL_MINIDLE = Integer.valueOf(bundle.getString("redis.pool.minIdle"));
		REDIS_POOL_MAXWAIT = Long.valueOf(bundle.getString("redis.pool.maxWait"));
		REDIS_POOL_TESTONBORROW = Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow"));
		REDIS_POOL_TESTONRETURN = Boolean.valueOf(bundle.getString("redis.pool.testOnReturn"));
		REDIS_POOL_TESTWHILEIDLE = Boolean.valueOf(bundle.getString("redis.pool.testWhileidle"));
		
		REDIS_SERVER_ONE_ADRESS = String.valueOf(bundle.getString("redis.server.one.address"));
		REDIS_SERVERIP_ONE_PORT = Integer.valueOf(bundle.getString("redis.server.one.port"));
		REDIS_SERVERIP_ONE_PWD = String.valueOf(bundle.getString("redis.server.one.pwd"));
		
		REDIS_SERVER_TWO_ADRESS = String.valueOf(bundle.getString("redis.serverip.two.address"));
		REDIS_SERVERIP_TWO_PORT =  Integer.valueOf(bundle.getString("redis.server.two.port"));
		REDIS_SERVERIP_TWO_PWD = String.valueOf(bundle.getString("redis.server.two.pwd"));
		
		} catch (Exception ex) {
			logger.error("ShardedJedisPool init shardedpool is error" + ex);
		}
	}
	
	public synchronized static void init(){
		//关闭之前的链接
		if(pool  != null){
			pool.destroy();
		}
		
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(Integer.valueOf(REDIS_POOL_MAXACTIVE));
		config.setMaxIdle(REDIS_POOL_MAXIDLE);
		config.setMinIdle(REDIS_POOL_MINIDLE);
		config.setMaxWait(REDIS_POOL_MAXWAIT);
		config.setTestOnBorrow(REDIS_POOL_TESTONBORROW);
		config.setTestOnReturn(REDIS_POOL_TESTONRETURN);
		config.setTestWhileIdle(REDIS_POOL_TESTWHILEIDLE);
		 // slave链接 
		JedisShardInfo jedisShardInfo1 = new JedisShardInfo(REDIS_SERVER_ONE_ADRESS, REDIS_SERVERIP_ONE_PORT);
		if(StringUtils.isNotEmpty(REDIS_SERVERIP_ONE_PWD)){
			jedisShardInfo1.setPassword(REDIS_SERVERIP_ONE_PWD);
		}
		shardInfos.add(jedisShardInfo1);
		
		JedisShardInfo jedisShardInfo2 = new JedisShardInfo(REDIS_SERVER_TWO_ADRESS, REDIS_SERVERIP_TWO_PORT);
		if(StringUtils.isNotEmpty(REDIS_SERVERIP_TWO_PWD)){
			jedisShardInfo2.setPassword(REDIS_SERVERIP_TWO_PWD);
		}
		shardInfos.add(jedisShardInfo2);
		// 构造池 
		pool = new ShardedJedisPool(config, shardInfos);		
	}

	// 从池中获取一个JedisPool对象
	public synchronized static ShardedJedisPool getShardedJedisPool() {
		if(pool == null){
			init();
		}
		return pool;
	}

	public static void main(String args[]) {
        ShardedJedis jedis = ShardedJedisPoolUtils.getShardedJedisPool().getResource();
        jedis.set("helo", "kiki");
        ShardedJedisPoolUtils.getShardedJedisPool().returnResource(jedis);
        System.out.println(jedis.get("helo"));

    }
}
